package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DQL语句
 */
public class JDBCDemo3 {
    public static void main(String[] args) {
        try (
                // Connection可以利用自动关闭的特性，使用完毕后关闭它
                Connection connection = DBUtil.getConnection();
        ) {
            // 查询老师的id，name,salary,age
            Statement statement = connection.createStatement();
            /*
                SELECT id,name,salary,age
                FROM teacher
             */
            String sql = "SELECT id,name,salary,age " +
                    "                FROM teacher";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                // int id = resultSet.getInt(1); // 也可以使用指定字段下标，数据库下标从1开始
                String name = resultSet.getString("name");
                int salary = resultSet.getInt("salary");
                int age = resultSet.getInt("age");
                System.out.println(id+","+name+","+salary+","+age);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
